package com.cc.spring.controller;

import com.cc.spring.common.CommonResp;
import com.cc.spring.request.DirRequest;
import com.cc.spring.request.UserRequest;
import com.cc.spring.resp.HdfsFileStatus;
import com.cc.spring.utils.HdfsUtil;
import org.apache.hadoop.fs.Path;
import org.checkerframework.checker.units.qual.C;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/file")
public class DiskController {
    private static final Logger LOG = LoggerFactory.getLogger(HdfsUtil.class);


    @Autowired
    private HdfsUtil hdfsUtil;

//    文件暂存路径
    @Value("${file.uploadPath}")
    private String UPLOADPATH;


    /**
     *
     * @param file 所上传的文件
     * @param userPath 用户的为一名字，其实就是用户的根目录
     * @return 上传是否成功
     */
//    测试上传
    @PostMapping("/upload")
    public CommonResp fileUpload(@RequestParam("file") MultipartFile file, String userPath){
        CommonResp commonResp = new CommonResp();
        if(file.isEmpty()){
            commonResp.setSuccess(false);
            commonResp.setMessage("上传文件为空");
            return commonResp;
        }
        File path = new File(this.UPLOADPATH , file.getOriginalFilename());
        try {
            file.transferTo(path);// 写入本地，暂时存放，之后写一个定时任务清空本地存储
            Path path1 = new Path(path.getPath()); //获取本地文件
            hdfsUtil.upload(path1, file.getOriginalFilename(), userPath);
            commonResp.setMessage("上传文件成功");
            return commonResp;

        } catch (IOException e) {
            e.printStackTrace();
            commonResp.setMessage("上传失败");
            commonResp.setSuccess(false);
            return commonResp;
        }

    }

    /**
     *
     * @param userRequest 文件请求参数
     * @return  文件相关信息
     */
    @GetMapping("/list")
//    获取指定用户的指定目录文件列表
    public CommonResp<List<HdfsFileStatus>> getFileList(UserRequest userRequest){
        CommonResp<List<HdfsFileStatus>> commonResp = new CommonResp();
//        System.out.println(userRequest);
        if(ObjectUtils.isEmpty(userRequest)){
            throw new RuntimeException("用户不能为空");
        }

        List<HdfsFileStatus> hdfsFileStatusList = hdfsUtil.queryFileByUser(userRequest);
        commonResp.setContent(hdfsFileStatusList);
        return commonResp;
    }

    /**
     *
     * @param userRequest 用户名及文件路径
     * @return 返回一个相应实体，携带文件资源
     */
//    文件下载
    @GetMapping("/download")
    public ResponseEntity<InputStreamResource> fileDownload(UserRequest userRequest){
//        防止别人直接请求接口导致系统崩溃
        if(ObjectUtils.isEmpty(userRequest)){
            LOG.info("请求下载的文件不存在userRequest{}",userRequest);
            return null;
        }
        ResponseEntity<InputStreamResource> isr = hdfsUtil.downFile(userRequest.getUserName(), userRequest.getFilePath());
      
        if(isr == null){
            return null;
        }
        return isr;
    }

//    创建目录
    @PostMapping("/dir")
    public CommonResp userMkdirs(@RequestBody DirRequest dirRequest){
        CommonResp commonResp = new CommonResp();
        boolean isMkdirs = hdfsUtil.mkdirs(dirRequest.getUserName(), dirRequest.getDirName());
//        如果创建失败
        if(!isMkdirs){
            commonResp.setMessage("创建目录失败");
        }
        commonResp.setMessage("创建目录成功");
        return commonResp;
    }

//    删除文件
    @DeleteMapping("/del")
    public CommonResp deleteFile(@RequestBody UserRequest userRequest){
        CommonResp commonResp = new CommonResp();
        boolean b = hdfsUtil.deleteFile(userRequest.getUserName(), userRequest.getFilePath());
        if(b) {
            commonResp.setMessage("删除失败");
            commonResp.setSuccess(false);
        }
        commonResp.setSuccess(true);

        commonResp.setMessage("删除成功");
        return commonResp;
    }

}
